import * as Api from '@/api/system/login'
import util from '@/utils/util'

const login = {
  state: {},
  mutations: {},
  actions: {
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        Api.login(data)
          .then(res => {
            const resData = res.data
            util.setToken(resData.token)
            util.setUser(resData.userInfo)
            util.setMenuList(resData.menuList)
            util.setPermissionMap(resData.permissionMap)
            resolve(resData)
          })
          .catch(err => {
            reject(err)
          })
      })
    },
    Logout({ commit }) {
      return new Promise((resolve, reject) => {
        Api.logout(util.getToken())
          .then(res => {
            resolve(res.data)
          })
          .catch(err => {
            reject(err)
          })
        util.removeUserInfo()
      })
    }
  }
}

export default login
